package com.bot.nft.module.dc.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bot.common.exception.CommonException;
import com.bot.nft.module.dc.domain.DcNftCert;
import com.bot.nft.module.dc.domain.DcSellInfo;
import com.bot.nft.module.dc.domain.bo.DcSellInfoBo;
import com.bot.nft.module.dc.domain.bo.DcSellNftQueryBo;
import com.bot.nft.module.dc.domain.vo.DcSellInfoVo;
import com.bot.nft.module.dc.domain.vo.DcSellNftVo;
import com.bot.nft.module.dc.mapper.DcNftCertMapper;
import com.bot.nft.module.dc.mapper.DcSellInfoMapper;
import com.bot.nft.module.dc.service.IDcSellInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author RenXiaoZheng
 * @since 2024-01-24
 */
@Service
public class DcSellInfoServiceImpl extends ServiceImpl<DcSellInfoMapper, DcSellInfo> implements IDcSellInfoService {

    //日志
    private static final Log log = LogFactory.get();

    //mapper
    @Resource
    private DcSellInfoMapper dcSellInfoMapper;


    //藏品证书Mapper
    @Resource
    private DcNftCertMapper nftCertMapper;

    /**
     * 查询寄售藏品
     *
     * @param keyword  藏品名称关键字
     * @param pageNo   当前第几页
     * @param pageSize 每页显示多少条数据
     * @return
     */
    @Override
    public Page<DcSellInfoVo> queryDcSellInfo(String keyword, Integer pageNo,
                                              Integer pageSize) {
        DcSellInfoServiceImpl.log.info("开始查询寄售藏品信息根据藏品关键字{}", keyword);
        Page<DcSellInfoVo> page = new Page<>(pageNo, pageSize);
        Page<DcSellInfoVo> sellInfo = dcSellInfoMapper.getSellInfo(keyword, page);
        DcSellInfoServiceImpl.log.info("查询寄售藏品信息根据藏品关键字{}结束", keyword);
        return sellInfo;
    }

    /**
     * 查看寄售藏品
     *
     * @param id 寄售藏品信息标识
     * @return-
     */
    @Override
    public DcSellInfoVo getDcSellInfo(String id) {
        //开始根据id查看寄售藏品信息
        DcSellInfoServiceImpl.log.info("开始根据{}查询订单信息", id);
        //根据藏品信息id查看寄售藏品信息
        DcSellInfoVo dcSellInfoVo = dcSellInfoMapper.lookDcSell(id);
        //根据藏品证书标识查询藏品证书
        return dcSellInfoVo;
    }


    @Override
    public Page<DcSellNftVo> pageMarketNft(Integer pageNo, Integer pageSize, DcSellNftQueryBo dcSellNftQueryBo) {
        //1.分页
        Page<DcSellNftVo> dcSellNftVoPage = new Page<>(pageNo, pageSize);
        //2.查询二级市场艺术品信息
        String nftName = dcSellNftQueryBo.getNftName();//藏品名称
        Integer nftType = dcSellNftQueryBo.getNftType();//藏品类型
        Integer partition = dcSellNftQueryBo.getPartition();//藏品分区
        Integer sort = dcSellNftQueryBo.getSort();//藏品排序方式
        dcSellInfoMapper.querySellNft(dcSellNftVoPage, nftName, nftType,
                partition, sort);
        return dcSellNftVoPage;
    }

    @Override
    public DcSellNftVo viewMarketNft(String nftId) {

        return dcSellInfoMapper.viewSellNft(nftId);
    }

    @Override
    public Page<DcSellInfoVo> pageDcSellInfo(Integer pageNo, Integer pageSize, String nftId) {
        //1.开启分页
        Page<DcSellInfoVo> dcSellInfoVoPage = new Page<>(pageNo, pageSize);
        //2.查询二级市场艺术品（证书）
        dcSellInfoMapper.queryDcSellInfo(dcSellInfoVoPage, nftId);

        return dcSellInfoVoPage;
    }

    /**
     * 寄售藏品信息添加寄售藏品
     *
     * @param dcSellInfoBo
     * @return
     */
    @Override
    public Boolean insertSellInfo(DcSellInfoBo dcSellInfoBo) {

        //entity转Bo
        DcSellInfo dcSellInfo = BeanUtil.copyProperties(dcSellInfoBo, DcSellInfo.class);
        dcSellInfo.setStatus(0);
        dcSellInfo.setConsignTime(new DateTime());
        int insert = dcSellInfoMapper.insert(dcSellInfo);
        if (insert > 0) {
            DcNftCert dcNftCert = new DcNftCert();
            dcNftCert.setId(dcSellInfo.getCertId());
            dcNftCert.setStatus(1);
            int i = nftCertMapper.updateById(dcNftCert);
            if (i <= 0) {
                throw new CommonException("寄售失败");
            }
        }
        return insert > 0;
    }

    @Override
    public DcSellInfoVo viewDcSellInfo(String id) {
        return dcSellInfoMapper.viewDcSellInfo(id);
    }


}
